<template>
  <main-box>
    <PlusSearch
      v-model="queryform"
      :columns="formConfig"
      :show-number="2"
      label-width="80"
      label-position="right"
      class="mb-xs"
      @search="initData()"
      @reset="handleRest"
    >
      <template #plus-field-fixResult>
        <el-radio-group v-model="queryform.fixResult">
          <el-radio-button label="全部" :value="-1" />
          <el-radio-button label="恢复正常 " :value="1" />
          <el-radio-button label="工具报废" :value="2" />
        </el-radio-group>
      </template>
      <template #plus-field-warehouseId>
        <chooseWarehouse v-model="queryform.warehouseId" />
      </template>
    </PlusSearch>
    <div class="mb-2">
      <btnExport
        :exportApi="exportApi"
        :queryform="getParams"
        download-name="维修记录"
      />
    </div>
    <PureTable
      ref="tableRef"
      border
      adaptive
      :loading="loading"
      :data="tableData"
      :columns="columns"
      :pagination="pagination"
      @page-size-change="initData()"
      @page-current-change="initData(false)"
    />
  </main-box>
</template>

<script setup lang="tsx">
import { onMounted, ref } from "vue";
import { useTableConfig } from "@/utils/tableConfig";
import { type PlusColumn, PlusSearch } from "plus-pro-components";

import { listApi, exportApi } from "@/api/overhaul/maintain";
import { diffDate } from "@/utils";
import btnExport from "@/components/btnExport/index.vue";
import chooseWarehouse from "@/components/choose-warehouse/index.vue";

/* 查询表单 */
const formConfig: PlusColumn[] = [
  {
    label: "完成时间",
    prop: "time",
    valueType: "date-picker",
    fieldProps: {
      type: "daterange",
      valueFormat: "YYYY-MM-DD"
    }
  },
  {
    label: "维修结果",
    prop: "fixResult"
  },
  {
    label: "工具室",
    prop: "warehouseId"
  },
  {
    label: "查询",
    prop: "searchValue",
    fieldProps: {
      placeholder: "名称/编号/类型"
    }
  }
];

const queryform = ref({
  time: [],
  fixResult: -1,
  searchValue: "",
  warehouseId: ""
});
const handleRest = () => {
  initData(true);
  queryform.value.fixResult = -1;
};

const getParams = () => {
  const params = {
    ...queryform.value
  } as any;
  delete params.time;
  if (queryform.value.time && queryform.value.time.length) {
    params.params = {
      startTime: queryform.value.time[0],
      endTime: queryform.value.time[1]
    };
  }
  if (params.fixResult == -1) {
    params.fixResult = "";
  }
  return params;
};

/* 表格数据 */
const { pagination } = useTableConfig();
const tableData = ref([]);
const loading = ref(false);
const columns = [
  {
    label: "物件图片",
    prop: "picUrl",
    cellRenderer: ({ row }) => {
      const { picUrl } = row;
      let list = [];
      if (picUrl) {
        list = picUrl.split(",");
      }
      return list.map((item, index) => {
        return (
          <el-image
            src={item}
            width={50}
            initial-index={index}
            preview-src-list={list}
            preview-teleported={true}
          />
        );
      });
    }
  },
  { label: "物件编号", prop: "code" },
  { label: "物件名称", prop: "toolName" },
  { label: "物件型号", prop: "modelName" },
  { label: "标签编码", prop: "labelCode" },
  { label: "工具室", prop: "warehouseName" },
  { label: "存放位置", prop: "storageLocation" },
  { label: "报修人", prop: "reportUserNickName" },
  { label: "报修时间", prop: "reportFixTime" },
  {
    label: "维修结果",
    prop: "fixResult",
    cellRenderer: ({ row }) => {
      const col = row.fixResult == 1 ? "success" : "danger";
      const txt = row.fixResult == 1 ? "恢复正常 " : "工具报废";
      return <el-tag type={col}>{txt}</el-tag>;
    }
  },
  { label: "维修人", prop: "fixUserNickName" },
  { label: "完成时间", prop: "finishTime" }
];
const initData = async (needInit = true) => {
  loading.value = true;
  if (needInit) {
    pagination.currentPage = 1;
  }
  const params = {
    ...getParams(),
    pageNum: pagination.currentPage,
    pageSize: pagination.pageSize
  } as any;
  await listApi(params).then(res => {
    tableData.value = res.rows;
    pagination.total = Number(res.total);
  });
  loading.value = false;
};
initData();
</script>
